/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: Entity.java 72 2007-01-06 22:09:10Z xdracox $
*/

/**	The AstroAttack package contains all the classes that belong to the
*	AstroAttack program.
*/
package AstroAttack;

import java.awt.Canvas;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**	The Entity class represents any object that can be moved and displayed
*	anywhere on the screen.
*/
public class Entity
{
	/**	The image this Entity is displayed as. */
	private Image m_image;
	/** The position of this entity. */
	private Point2D m_point;
	/**	Direction the entity is moving in. */
	private double m_direction;
	/**	The speed the entity is moving at. */
	private double m_speed;
	/**	The health of this Entity. */
	private int m_health;
	
	/**	The constructor initializes the Entity's image representation
	*	and it's location on the screen.
	*/
	public Entity(Image image, Point2D point, int initialHealth)
	{
		m_image = image;
		m_point = point;
		m_health = initialHealth;
	}
	
	/**	Moves the Entity to where it should be according to the delta parameter
	*	@param	delta	a representation of how many canvass have passed since
	*					the last time this entity was moved
	*/
	public void move()
	{
		double xSpeed = m_speed * Math.cos(Math.toRadians(m_direction));
		double ySpeed = -m_speed * Math.sin(Math.toRadians(m_direction));
		
		double newXPos = m_point.getX() + xSpeed;
		double newYPos = m_point.getY() + ySpeed;
		
		m_point.setLocation(newXPos, newYPos);
		
		Canvas canvas = Engine.getEngine().getCanvas();
		
		if( newXPos > canvas.getWidth() || newYPos > canvas.getHeight() || newYPos<-64 || newXPos<-64)
			m_health = 0;
	}
	
	/**	Renders (draws) the image using the provided graphics
	*	@param	gfx	the Graphics to use when rendering
	*/
	public void render(Graphics2D gfx)
	{
		/**	@todo	there must be better way to draw the image */
		gfx.drawImage(m_image, (int)m_point.getX(), (int)m_point.getY(), null);
	}
	
	/**	Returns the bounding rectangle that represents where the Entity is and
	*	how much space it takes up.
	*	@return	a Rectangle2D.Double
	*/
	public Rectangle2D getBounds()
	{
		double w = m_image.getWidth(null);
		double h = m_image.getHeight(null);
		double x = m_point.getX();
		double y = m_point.getY();
		
		return new Rectangle2D.Double(x, y, w, h);
	}
	
	public int getX()
	{
		return (int)m_point.getX();
	}
	
	public int getY()
	{
		return (int)m_point.getY();
	}
	
	/**	Sets the image that represents this Entity
	*	@param	img	the new image
	*/
	public void setImage(Image img)
	{
		if ( img == null )
			throw new IllegalArgumentException("Parameter img cannot be null");
		m_image = img;
	}
	
	public Image getImage()
	{
		return m_image;
	}
	
	/**	Returns wether or not this entity is alive.
	*	@return	true if alive, false if dead
	*/
	public boolean isAlive()
	{
		return m_health > 0;
	}
	
	/**	Sets the direction of this entity.
	*
	*	The direction being in degrees and east being 0 going counter-clockwise
	*
	*	@param	direction	a double representing the direction
	*/
	public void setDirection(double direction)
	{
		while ( direction >= 360 )
			direction -= 360;
		
		while ( direction < 0 )
			direction += 360;
		
		m_direction = direction;
	}
	
	/**	Returns the direction of this entity
	*	@return	direction in degrees
	*	@see	setDirection(double)
	*/
	public double getDirection()
	{
		return m_direction;
	}
	
	/**	Sets the speed this Entity is moving at.
	*	@param	speed	the new speed of the entity
	*/
	public void setSpeed(double speed)
	{
		m_speed = speed;
	}
	
	/**	Returns the speed of the entity
	*	@return	speed
	*/
	public double getSpeed()
	{
		return m_speed;
	}
	
	/** Sets the health of this entity
	*	@param	health	the new health
	*/
	public void setHealth(int health)
	{
		m_health = health;
	}
	
	/** Sets the health of this entity
	*	@param	health	the new health
	*/
	public void shot(int damage)
	{
		m_health -= damage;
	}
	
	/**	Returns the health of this entity
	*	@return	health
	*/
	public int getHealth()
	{
		return m_health;
	}
	
	/**	Sets the point of where the Entity is
	*	@param	point	the new location
	*/
	public void setPoint(Point2D point)
	{
		m_point = point;
	}
	
	/**	Returns the point of the entity
	*	@return	point
	*/
	public Point2D getPoint()
	{
		return m_point;
	}
}
